﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TaxiDispatchClient
{
    public partial class TradeHistoryForm : DevExpress.XtraEditors.XtraForm
    {
        public TradeHistoryForm()
        {
            InitializeComponent();
            dateEdit_start.DateTime = DateTime.Now.Date.AddDays(-5);
            dateEdit_end.DateTime = DateTime.Now;
            gridView1.GroupPanelText = "正在加载数据...";
            btn_select.Enabled = false;
            _thred = new Thread(BindData);
            _thred.Start();
        }
        
        private string _title;
        private Thread _thred;

        private void BindData()
        {
            try
            {
                string parameter;
                DateTime beginDate = dateEdit_start.DateTime;
                DateTime endDate = dateEdit_end.DateTime;
             
                parameter = " tradetime>='" + beginDate.ToString() +
               "' and tradetime<='" + endDate.ToString() + "' ";
             
                if (radioGroup_caller.SelectedIndex == 0)
                {
                    parameter += "and Type=1 ";
                }
                else if (radioGroup_caller.SelectedIndex == 1)
                {
                    parameter += "and Type=2 ";
                }

                if (textEdit_phone.Text.Trim() != "")
                {
                    parameter += "and phoneNumber like '%" +
                        textEdit_phone.Text.Trim() + "%'";
                }

                DataSet ds = MainForm.PositionService.TradeGetList(parameter);
                this.BeginInvoke(new MethodInvoker(() =>
                                                       {
                                                           gridControl1.DataSource = ds.Tables[0];
                                                           gridView1.GroupPanelText = "共计" + ds.Tables[0].Rows.Count + "条记录";
                                                           _title = dateEdit_start.DateTime.ToString("yyyy年MM月dd号HH点mm分") +
                                                              "至" + dateEdit_end.DateTime.ToString("yyyy年MM月dd号HH点mm分") +
                                                          "招车记录";
                                                           btn_select.Enabled = true;
                                                       }));
              
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);
                BeginInvoke(new MethodInvoker(() => btn_select.Enabled =true));
            }

          
        }

        private void pictureEdit1_Click(object sender, EventArgs e)
        {
            if(CheckPass())
            {
                btn_select.Enabled = false;
                BindData();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }


         #region   窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }
        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        
#endregion


        private bool  CheckPass()
        {
            DateTime beginDate = dateEdit_start.DateTime;
            DateTime endDate = dateEdit_end.DateTime;
            if (beginDate > endDate)
            {
                XtraMessageBox.Show("结束日期不能大于起始日期，请重新输入");
                dateEdit_start.Focus();
                return false;
            }
            return true;
        }
   
        private void btn_exportToExcel_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = _title;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private void btn_report_Click(object sender, EventArgs e)
        {
            TradeHistoryReportForm reportForm = new TradeHistoryReportForm();
            reportForm.ShowDialog();
        }

        private void TradeHistoryForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(_thred!=null)
            {
                _thred.Abort();
            }
        } 

    }
}